<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form [formGroup]="timewindowForm" class="tb-timewindow-form">
  <div class="tb-timewindow-form-header tb-form-panel no-border no-padding-bottom" *ngIf="!historyOnly">
    <tb-toggle-select class="tb-timewindow-form-type-options" appearance="fill"
                      [options]="timewindowTypeOptions" formControlName="selectedTab">
    </tb-toggle-select>
    <button mat-icon-button type="button" class="tb-timewindow-form-settings-btn tb-mat-24"
            *ngIf="isEdit"
            (click)="openTimewindowConfig()">
      <mat-icon>settings</mat-icon>
    </button>
  </div>

  <div class="tb-timewindow-form-content tb-form-panel no-border" [class.no-padding]="!panelMode">
    <ng-container *ngIf="timewindowForm.get('selectedTab').value === timewindowTypes.REALTIME">
      <section class="tb-form-panel stroked" *ngIf="realtimeIntervalSelectionAvailable; else timezoneSelectionPanel">
        <div class="tb-flex space-between"
             [class.align-end]="realtimeTypeSelectionAvailable"
             [class.align-center]="!realtimeTypeSelectionAvailable">
          <div class="tb-flex-xs column">
            <div class="tb-form-panel-title">{{ 'timewindow.timewindow' | translate }}</div>
            <ng-container formGroupName="realtime" *ngIf="realtimeTypeSelectionAvailable">
              <tb-toggle-select appearance="stroked" [options]="realtimeTimewindowOptions" formControlName="realtimeType">
              </tb-toggle-select>
            </ng-container>
          </div>

          <ng-container *ngTemplateOutlet="timezoneSelection">
          </ng-container>
        </div>

        <div class="tb-form-row no-border no-padding" formGroupName="realtime">
          <tb-timeinterval
            *ngIf="timewindowForm.get('realtime.realtimeType').value === realtimeTypes.LAST_INTERVAL"
            formControlName="timewindowMs"
            subscriptSizing="dynamic"
            appearance="outline"
            [allowedIntervals]="realtimeAllowedLastIntervals"
            [disabledAdvanced]="realtimeDisableCustomInterval"
            [required]="timewindowForm.get('selectedTab').value === timewindowTypes.REALTIME &&
                              timewindowForm.get('realtime.realtimeType').value === realtimeTypes.LAST_INTERVAL">
          </tb-timeinterval>

          <tb-quick-time-interval
            *ngIf="timewindowForm.get('realtime.realtimeType').value === realtimeTypes.INTERVAL"
            displayLabel="false"
            formControlName="quickInterval"
            onlyCurrentInterval="true"
            subscriptSizing="dynamic"
            appearance="outline"
            [allowedIntervals]="realtimeAllowedQuickIntervals"
            [required]="timewindowForm.get('selectedTab').value === timewindowTypes.REALTIME &&
                              timewindowForm.get('realtime.realtimeType').value === realtimeTypes.INTERVAL">
          </tb-quick-time-interval>
        </div>
      </section>
    </ng-container>

    <ng-container *ngIf="timewindowForm.get('selectedTab').value === timewindowTypes.HISTORY">
      <section class="tb-form-panel stroked" *ngIf="historyIntervalSelectionAvailable; else timezoneSelectionPanel">
        <div class="tb-flex space-between"
             [class.align-end]="historyTypeSelectionAvailable"
             [class.align-center]="!historyTypeSelectionAvailable">
          <div class="tb-flex-xs column">
            <div class="tb-form-panel-title">{{ 'timewindow.timewindow' | translate }}</div>
            <ng-container formGroupName="history" *ngIf="historyTypeSelectionAvailable">
              <tb-toggle-select appearance="stroked" [options]="historyTimewindowOptions" formControlName="historyType">
              </tb-toggle-select>
            </ng-container>
          </div>

          <ng-container *ngTemplateOutlet="timezoneSelection">
          </ng-container>
        </div>

        <div class="tb-form-row no-border no-padding"
             formGroupName="history" *ngIf="historyIntervalSelectionAvailable &&
                                  timewindowForm.get('history.historyType').value !== historyTypes.FOR_ALL_TIME">
          <tb-timeinterval
            *ngIf="timewindowForm.get('history.historyType').value === historyTypes.LAST_INTERVAL"
            formControlName="timewindowMs"
            subscriptSizing="dynamic"
            appearance="outline"
            [allowedIntervals]="historyAllowedLastIntervals"
            [disabledAdvanced]="historyDisableCustomInterval"
            [required]="timewindowForm.get('selectedTab').value === timewindowTypes.HISTORY &&
                            timewindowForm.get('history.historyType').value === historyTypes.LAST_INTERVAL">
          </tb-timeinterval>

          <tb-datetime-period
            *ngIf="timewindowForm.get('history.historyType').value === historyTypes.FIXED"
            formControlName="fixedTimewindow"
            subscriptSizing="dynamic"
            appearance="outline"
            class="history-time-input"
            [required]="timewindowForm.get('selectedTab').value === timewindowTypes.HISTORY &&
                            timewindowForm.get('history.historyType').value === historyTypes.FIXED">
          </tb-datetime-period>

          <tb-quick-time-interval
            *ngIf="timewindowForm.get('history.historyType').value === historyTypes.INTERVAL"
            displayLabel="false"
            formControlName="quickInterval"
            subscriptSizing="dynamic"
            appearance="outline"
            [allowedIntervals]="historyAllowedQuickIntervals"
            [required]="timewindowForm.get('selectedTab').value === timewindowTypes.HISTORY &&
                          timewindowForm.get('history.historyType').value === historyTypes.INTERVAL">
          </tb-quick-time-interval>
        </div>
      </section>
    </ng-container>

    <ng-container *ngIf="aggregationOptionsAvailable">
      <ng-container formGroupName="aggregation">
        <section class="tb-form-row column-xs space-between same-padding" *ngIf="isEdit || !timewindow.hideAggregation">
          <div class="fixed-title-width-180">{{ 'aggregation.aggregation' | translate }}</div>
          <tb-aggregation-type-select class="flex" subscriptSizing="dynamic" appearance="outline" displayLabel="false"
                                      formControlName="type" [allowedAggregationTypes]="allowedAggTypes">
          </tb-aggregation-type-select>
        </section>

        <section class="tb-form-row column-xs space-between same-padding"
                 *ngIf="timewindowForm.get('aggregation.type').value === aggregationTypes.NONE && (isEdit || !timewindow.hideAggInterval)">
          <div>{{ 'aggregation.limit' | translate }}</div>
          <tb-datapoints-limit formControlName="limit"
                               [required]="timewindowForm.get('aggregation.type').value === aggregationTypes.NONE">
          </tb-datapoints-limit>
        </section>
      </ng-container>

      <section class="tb-form-row column-xs same-padding" *ngIf="timewindowForm.get('aggregation.type').value !== aggregationTypes.NONE"
               [class.!hidden]="!(isEdit || !timewindow.hideAggInterval)">
        <ng-container formGroupName="realtime" *ngIf="timewindowForm.get('selectedTab').value === timewindowTypes.REALTIME">
          <tb-timeinterval
            formControlName="interval"
            [min]="minRealtimeAggInterval" [max]="maxRealtimeAggInterval"
            [allowedIntervals]="realtimeAllowedAggIntervals"
            useCalendarIntervals
            subscriptSizing="dynamic"
            appearance="outline"
            [disabledAdvanced]="realtimeDisableCustomGroupInterval">
            <div class="fixed-title-width-180">{{ 'aggregation.group-interval' | translate }}</div>
          </tb-timeinterval>
        </ng-container>
        <ng-container formGroupName="history" *ngIf="timewindowForm.get('selectedTab').value === timewindowTypes.HISTORY">
          <tb-timeinterval
            formControlName="interval"
            [min]="minHistoryAggInterval" [max]="maxHistoryAggInterval"
            [allowedIntervals]="historyAllowedAggIntervals"
            useCalendarIntervals
            subscriptSizing="dynamic"
            appearance="outline"
            [disabledAdvanced]="historyDisableCustomGroupInterval">
            <div class="fixed-title-width-180">{{ 'aggregation.group-interval' | translate }}</div>
          </tb-timeinterval>
        </ng-container>
      </section>
    </ng-container>
    @if (saveAsDefaultAvailable) {
      <mat-checkbox class="tb-timewindow-form-checkbox" [formControl]="saveTimewindowControl">
        {{ 'timewindow.save-current-settings-as-default' | translate }}
      </mat-checkbox>
    }
  </div>

  <ng-template #timezoneSelectionPanel>
    <section class="tb-form-row space-between same-padding">
      <div>{{ 'timezone.timezone' | translate }}</div>
      <ng-container *ngTemplateOutlet="timezoneSelection">
      </ng-container>
    </section>
  </ng-template>
  <ng-template #timezoneSelection>
    <tb-timezone *ngIf="timezone && (isEdit || !timewindow.hideTimezone)"
                 asButton strokedButton noMargin
                 localBrowserTimezonePlaceholderOnEmpty="true"
                 formControlName="timezone">
    </tb-timezone>
  </ng-template>
  <mat-divider *ngIf="panelMode"></mat-divider>
  <div class="tb-panel-actions tb-flex flex-end no-gap" *ngIf="panelMode">
    <button type="button"
            mat-button
            [disabled]="(isLoading$ | async)"
            (click)="cancel()">
      {{ 'action.cancel' | translate }}
    </button>
    <button type="button"
            mat-raised-button
            color="primary"
            (click)="update()"
            [disabled]="(isLoading$ | async) || timewindowForm.invalid || !timewindowForm.dirty">
      {{ 'action.update' | translate }}
    </button>
  </div>
</form>
